package com.innovation.chengzhangriji.ui.widget;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class HistoryPhotoContentAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public interface OnClickHistoryPhotoListener {
        void onClickHistoryPhoto(RecyclerView.ViewHolder holder, int index);
    }

    private OnClickHistoryPhotoListener mOnClickHistoryPhotoListener;

    private ImageView mImageThumbnail1;

    private ImageView mImageThumbnail2;

    private ImageView mImageThumbnail3;

    private ImageView mImageThumbnail4;

    private boolean mIsLocal;

    public HistoryPhotoContentAdapterViewHolder(View itemView) {
        super(itemView);
        LinearLayout layout = (LinearLayout) itemView.findViewById(R.id.layout_history);
        DisplayMetrics dm = itemView.getContext().getResources().getDisplayMetrics();
        int w = (int) (dm.widthPixels - dm.density * 20);
        int h = w / 4;
        layout.setLayoutParams(new RecyclerView.LayoutParams(w, h));
        mImageThumbnail1 = (ImageView) itemView.findViewById(R.id.image_thumbnail_1);
        mImageThumbnail2 = (ImageView) itemView.findViewById(R.id.image_thumbnail_2);
        mImageThumbnail3 = (ImageView) itemView.findViewById(R.id.image_thumbnail_3);
        mImageThumbnail4 = (ImageView) itemView.findViewById(R.id.image_thumbnail_4);
        mImageThumbnail1.setOnClickListener(this);
        mImageThumbnail2.setOnClickListener(this);
        mImageThumbnail3.setOnClickListener(this);
        mImageThumbnail4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mOnClickHistoryPhotoListener == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.image_thumbnail_1:
                mOnClickHistoryPhotoListener.onClickHistoryPhoto(this, 0);
                break;
            case R.id.image_thumbnail_2:
                mOnClickHistoryPhotoListener.onClickHistoryPhoto(this, 1);
                break;
            case R.id.image_thumbnail_3:
                mOnClickHistoryPhotoListener.onClickHistoryPhoto(this, 2);
                break;
            case R.id.image_thumbnail_4:
                mOnClickHistoryPhotoListener.onClickHistoryPhoto(this, 3);
                break;
        }
    }

    public void setOnClickHistoryPhotoListener(OnClickHistoryPhotoListener onClickHistoryPhotoListener) {
        mOnClickHistoryPhotoListener = onClickHistoryPhotoListener;
    }

    public void setThumbnailUrls(String[] thumbnailUrls) {
        displayImage(thumbnailUrls[0], mImageThumbnail1);
        displayImage(thumbnailUrls[1], mImageThumbnail2);
        displayImage(thumbnailUrls[2], mImageThumbnail3);
        displayImage(thumbnailUrls[3], mImageThumbnail4);
    }

    public void setLocal(boolean isLocal) {
        mIsLocal = isLocal;
    }

    private void displayImage(String thumbnailUrl, ImageView imageView) {
        if (TextUtils.isEmpty(thumbnailUrl)) {
            imageView.setVisibility(View.INVISIBLE);
            return;
        }
        imageView.setVisibility(View.VISIBLE);

        if (mIsLocal) {
            ImageLoader.getInstance().displayImage("file://" + thumbnailUrl, imageView, Config.getOptionsPhoto(), null);
        } else {
            ImageLoader.getInstance().displayImage(Config.createSizeUrl(thumbnailUrl, 480), imageView, Config.getOptionsPhoto(), null);
        }
    }
}
